package com.chinatsp.onlineapp.ui.limit_rule;

import tsp.cn.http.tools.PreferenceUtils;
import tsp.cn.http.tools.T;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.BaseActivity;
import com.chinatsp.onlineapp.presenter.LimitNumDao;
import com.chinatsp.onlineapp.ui.violation.SelectCityActivity;
import com.chinatsp.onlineapp.utils.ActivityUtil;

public class LimitNumActivity extends BaseActivity implements
		View.OnClickListener {

	private Button btnLimitRule;
	private ImageView ivBack;
	private Button btnCity;
	private LimitNumDao mLimitNumDao;
	TextView mTvWeekTitle;
	TextView mtvCarNumTitle;
	TextView mTvNumTitle;
	TextView mTvNumTitle1;
	TextView mTtvAnd;
	
	TextView tvTomorrowTitle;
	private int requestCode = 2;
	@Override
	public void onRequestSuccess(int requestCode) {
		super.onRequestSuccess(requestCode);
		// 开始更新UI
		showProgress(false);
		T.show(this, "message", 0);
		String Week_day = mLimitNumDao.getLimitNum().getResp_data().getWeek_day();
		mTvWeekTitle.setText(Week_day);
		String limit_scope = mLimitNumDao.getLimitNum().getResp_data().getLimit_scope();
		mtvCarNumTitle.setText(limit_scope);
		String limit_code = mLimitNumDao.getLimitNum().getResp_data().getLimit_code();
		if(limit_code.contains(",")){ 
			String[] limit_nums = limit_code.split(","); 
			if(limit_nums!=null&&limit_nums.length==2){
				mTvNumTitle.setText(limit_nums[0]);
				mTvNumTitle1.setText(limit_nums[1]);
				mTtvAnd.setText("&");
			}
		}else{
			mTvNumTitle.setText("");
			mTvNumTitle1.setText("");
			mTtvAnd.setText(limit_code);
		}
		String next_limit_code = mLimitNumDao.getLimitNum().getResp_data().getNext_limit_code();
		tvTomorrowTitle.setText("明日限行: "+next_limit_code);
	}

	@Override
	public void onRequestFailed(String errorNo, String errorMessage) {
		super.onRequestFailed(errorNo, errorMessage);
		// 开始更新UI
		showProgress(false);
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_limit_num);
		initView();
	}

	private void initView() {
		mLimitNumDao = new LimitNumDao(this, this);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		btnLimitRule = (Button) findViewById(R.id.btn_limit_rule);
		btnCity = (Button) findViewById(R.id.btn_city);
		mTvWeekTitle = (TextView) findViewById(R.id.tv_week_title);
		mtvCarNumTitle = (TextView) findViewById(R.id.tv_car_num_title);
		
		mTvNumTitle = (TextView) findViewById(R.id.tv_num_title);
		mTvNumTitle1 = (TextView) findViewById(R.id.tv_num_title1);
		tvTomorrowTitle = (TextView) findViewById(R.id.tv_tomorrow_limit_title);
		mTtvAnd = (TextView) findViewById(R.id.tv_and);
		showProgress(true);
		mLimitNumDao.getLimitNum("北京");

		ivBack.setOnClickListener(this);
		btnLimitRule.setOnClickListener(this);
		btnCity.setOnClickListener(this);
		
		String default_city = PreferenceUtils.getPreference("default_city",getString(R.string.default_city));
		btnCity.setText(default_city); 
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_limit_rule:
			ActivityUtil.intentActivity(LimitNumActivity.this,
					LimitRuleActivity.class, null);
			break;
			
		case R.id.iv_back:
			finish();
			break;
			
		case R.id.btn_city:
			Intent intent = new Intent();
			intent.setClass(this, SelectCityActivity.class);			
			startActivityForResult(intent, requestCode);

			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			String CityName = data.getStringExtra("selectCity");
			btnCity.setText(CityName);
				
		}
		
		
	}
}
